package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.CourseTypes;
import com.dingreading.cloud.admin.entity.table.CourseTypesTableDef;
import com.dingreading.cloud.admin.mapper.CourseTypesMapper;
import com.dingreading.cloud.admin.service.CourseTypesService;
import com.dingreading.cloud.common.util.NepUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CourseTypesServiceImpl extends BaseServiceImpl<CourseTypesMapper, CourseTypes, CourseTypesTableDef> implements CourseTypesService {

    @Override
    protected CourseTypesTableDef getTable() {
        return CourseTypesTableDef.courseTypes;
    }

    @Override
    public Long duplicationMainName(Long id, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.courseTypeName.eq(name))
                .and(table.parentId.eq(0L));
        if (id != null) {
            condition.and(table.id.ne(id));
        }

        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public int getMaxShowOrder(Long parentId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.showOrder))
                .where(table.parentId.eq(parentId));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);

        return NepUtil.nullToZero(integer);
    }

    @Override
    public void updateEnabled(Long id, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.mainId.eq(id));

        UpdateChain.of(CourseTypes.class)
                .set(table.enabled, enabled)
                .where(condition)
                .update();
    }

    @Override
    public List<CourseTypes> getMainCourseTypes(Integer enable) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.parentId.eq(0L));
        if (null != enable)
            condition.and(table.enabled.eq(enable));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.showOrder.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<CourseTypes> getByMainId(Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.mainId.eq(id));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.showOrder.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public long dropByParentId(Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id).or(table.parentId.eq(id)));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean updateNodes(Long id, String nodes) {
        return UpdateChain.of(CourseTypes.class)
                .set(table.nodes, nodes)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public boolean updateMainIdAndNodes(Long id, String nodes) {
        return UpdateChain.of(CourseTypes.class)
                .set(table.mainId, id)
                .set(table.nodes, nodes)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<CourseTypes> getByNodesNotId(String nodes, Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.nodes.likeLeft(nodes));
        if (id != null)
            condition.and(table.id.ne(id));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public CourseTypes getGtShowOrder(Long parentId, Integer showOrder) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.parentId.eq(parentId))
                .and(table.showOrder.gt(showOrder));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.showOrder.asc());

        return getMapper().selectOneByQuery(wrapper);
    }


    @Override
    public CourseTypes getLtShowOrder(Long parentId, Integer showOrder) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.parentId.eq(parentId))
                .and(table.showOrder.lt(showOrder));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.showOrder.desc());

        return getMapper().selectOneByQuery(wrapper);
    }


    @Override
    public void updateShowOrder(Long id, Integer orderNum) {
        UpdateChain.of(CourseTypes.class)
                .set(table.showOrder, orderNum)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<CourseTypes> getAllTypes() {
        QueryWrapper wrapper = QueryWrapper.create()
                .orderBy(table.parentId.asc(), table.levelIndex.asc(), table.showOrder.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean updateEnabledByParentId(Long id, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id).or(table.parentId.eq(id)));
        return UpdateChain.of(CourseTypes.class)
                .set(table.enabled, enabled)
                .where(condition)
                .update();
    }

    @Override
    public List<String> getByCourseTypeIdList(List<Long> courseTypeIdList) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.id.in(courseTypeIdList))
                .and(table.parentId.eq(0L));
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.courseTypeName)
                .where(condition);
        return getMapper().selectListByQueryAs(wrapper, String.class);
    }

}
